package com.hanyxx.io;

import java.io.*;
import java.util.Properties;

/**
 * @author layman
 * @date 2021/3/9
 */
public class Demo07 {
    private static Properties p = new Properties();
    private static FileOutputStream fos;
    private static FileWriter fw;

    private static FileInputStream fis;
    private static FileReader fr;
    static{
        // 设置key和value
        p.setProperty("name1","outerMan");
        p.setProperty("name2","layman");
        p.setProperty("name3","钢铁侠");
        p.setProperty("name4","葫芦娃");
    }

    public static void main(String[] args) throws IOException {
        //saveFromOutputStream();
        //saveFromWriter();
        readFromInputStream();
        //readFromReader();
    }

    // 以字节流形式存储，中文会显示乱码
    private static void saveFromOutputStream() throws IOException {
        fos = new FileOutputStream("pro_字节.txt");
        String comments = "Form OutputStream";
        p.store(fos,comments);
        fos.close();
    }
    // 以字符流形式存储
    private static void saveFromWriter() throws IOException {
        fw = new FileWriter("pro_字符.txt");
        // 注释信息使用的是unicode编码，所以使用中文注释会出现乱码
        String comments = "通过字符流存储";
        p.store(fw,comments);
        fw.close();
    }

    // 以字节流形式读取，中文会显示乱码
    private static void readFromInputStream() throws IOException {
        fis = new FileInputStream("pro_字节.txt");
        p.load(fis);
        fis.close();
        System.out.println(p);
    }

    // 以字符流形式读取
    private static void readFromReader() throws IOException {
        fr = new FileReader("pro_字符.txt");
        p.load(fr);
        fr.close();
        System.out.println(p);
    }
}
